package wheelview;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;

import com.concerneyes.R;

import util.ActivityUtil;
import view.MyUniversalDialog;

import android.database.sqlite.SQLiteDatabase;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;


import java.util.ArrayList;
import java.util.List;

import wheelview.CityDataHelper;
import wheelview.widget.OnWheelChangedListener;
import wheelview.widget.OnWheelScrollListener;
import wheelview.widget.WheelView;
import wheelview.widget.adapters.AreaAdapter;
import wheelview.widget.adapters.CitysAdapter;
import wheelview.widget.adapters.ProvinceAdapter;
import wheelview.widget.model.CityModel;
import wheelview.widget.model.DistrictModel;
import wheelview.widget.model.ProvinceModel;


public class DialogChooseCity implements View.OnClickListener, OnWheelChangedListener {
    private WheelView provinceView;
    private WheelView cityView;
    private WheelView districtView;
    private List<ProvinceModel> provinceDatas = new ArrayList<>();
    private List<CityModel> cityDatas = new ArrayList<>();
    private List<DistrictModel> districtDatas = new ArrayList<>();
    private String mCurrentProvince;
    private String mCurrentCity;
    private String mCurrentDistrict;
    private TextView btnSave;
    private ImageView pay_cancel;
    private ProvinceAdapter provinceAdapter;
    private CitysAdapter citysAdapter;
    private AreaAdapter areaAdapter;
    private SQLiteDatabase db;
    private CityDataHelper dataHelper;
    private final int TEXTSIZE = ActivityUtil.sp2px(6);//选择器的字体大小
    private Context context;
    private chooseCitySuccessListener listener;
    private MyUniversalDialog dialog;
    private int width;

    public DialogChooseCity(Context context, chooseCitySuccessListener listener, int width) {
        this.context = context;
        this.listener = listener;
        this.width = width;
        showChooseCityDialog();

    }

    public void dialogShow() {
        dialog.show();
    }

    private void showChooseCityDialog() {
        dialog = new MyUniversalDialog(context);
        dialog.setLayout(LayoutInflater.from(context).inflate(R.layout.activity_city_choose, null),
                MyUniversalDialog.DialogGravity.CENTER, -2, width);
        dialog.show();
        provinceView = (WheelView) dialog.findViewById(R.id.provinceView);
        cityView = (WheelView) dialog.findViewById(R.id.cityView);
        districtView = (WheelView) dialog.findViewById(R.id.districtView);
        btnSave = (TextView) dialog.findViewById(R.id.btn_save);
        pay_cancel = (ImageView) dialog.findViewById(R.id.pay_cancel);
        btnSave.setOnClickListener(this);
        pay_cancel.setOnClickListener(this);
        // 设置可见条目数量
        provinceView.setVisibleItems(3);
        cityView.setVisibleItems(3);
        districtView.setVisibleItems(3);
        // 添加change事件
        provinceView.addChangingListener(this);
        // 添加change事件
        cityView.addChangingListener(this);
        // 添加change事件
        districtView.addChangingListener(this);
        initData();

    }

    private void initData() {
        //初始化数据
        dataHelper = CityDataHelper.getInstance(context);
        db = dataHelper.openDataBase();
        provinceDatas = dataHelper.getProvice(db);
        Log.e(this.getClass().getSimpleName(), "initData: " + provinceDatas.toString());
        if (provinceDatas.size() > 0) {
            mCurrentProvince = provinceDatas.get(0).NAME;
            cityDatas = dataHelper.getCityByParentId(db, provinceDatas.get(0).CODE);
        }
        if (cityDatas.size() > 0) {
            districtDatas = dataHelper.getDistrictById(db, cityDatas.get(0).CODE);
        }
        provinceAdapter = new ProvinceAdapter(context, provinceDatas, 0);
        provinceAdapter.setTextSize(TEXTSIZE);//设置字体大小
        provinceView.setViewAdapter(provinceAdapter);
        mCurrentProvince = provinceDatas.get(provinceView.getCurrentItem()).NAME;
        mCurrentCity = cityDatas.get(cityView.getCurrentItem()).NAME;
        mCurrentDistrict = districtDatas.get(districtView.getCurrentItem()).NAME;
        updateCitys();
        updateAreas();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_save:
                listener.onChooseCitySuccess(mCurrentProvince, mCurrentCity, mCurrentDistrict);
                dialog.dismiss();
            case R.id.pay_cancel:
                dialog.dismiss();
        }
    }

    @Override
    public void onChanged(WheelView wheel, int oldValue, int newValue) {
        if (wheel == provinceView) {
            mCurrentProvince = provinceDatas.get(newValue).NAME;
            provinceAdapter.setCurrentIndex(newValue);
            updateCitys();

        }
        if (wheel == cityView) {
            mCurrentCity = cityDatas.get(newValue).NAME;
            citysAdapter.setCurrentIndex(newValue);
            updateAreas();
        }
        if (wheel == districtView) {
            mCurrentDistrict = districtDatas.get(newValue).NAME;
            areaAdapter.setCurrentIndex(newValue);
        }
    }

    private void updateAreas() {
        final int cCurrent = cityView.getCurrentItem();
        new Thread(new Runnable() {
            @Override
            public void run() {

                if (cityDatas.size() > 0) {
                    try {
                        districtDatas = dataHelper.getDistrictById(db, cityDatas.get(cCurrent).CODE);
                        Thread.sleep(100);
                        Message msg = new Message();
                        msg.what = 2; // 消息(一个整型值)
                        handler.sendMessage(msg);// 每隔1秒发送一个msg给mHandler
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    districtDatas.clear();
                }
            }
        }).start();
    }

    private void updateCitys() {
        final int pCurrent = provinceView.getCurrentItem();
        new Thread(new Runnable() {
            @Override
            public void run() {
                if (provinceDatas.size() > 0) {
                    try {
                        cityDatas = dataHelper.getCityByParentId(db, provinceDatas.get(pCurrent).CODE);
                        Thread.sleep(100);
                        Message msg = new Message();
                        msg.what = 1; // 消息(一个整型值)
                        handler.sendMessage(msg);// 每隔1秒发送一个msg给mHandler
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    cityDatas.clear();
                }
            }
        }).start();
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    citysAdapter = new CitysAdapter(context, cityDatas, 0);
                    citysAdapter.setTextSize(TEXTSIZE);
                    cityView.setViewAdapter(citysAdapter);
                    if (cityDatas.size() > 0) {
                        cityView.setCurrentItem(0);
                        mCurrentCity = cityDatas.get(0).NAME;
                    } else {
                        mCurrentCity = "";
                    }
                    updateAreas();
                    break;
                case 2:
                    areaAdapter = new AreaAdapter(context, districtDatas, 0);
                    areaAdapter.setTextSize(TEXTSIZE);
                    districtView.setViewAdapter(areaAdapter);
                    if (districtDatas.size() > 0) {
                        mCurrentDistrict = districtDatas.get(0).NAME;
                        districtView.setCurrentItem(0);
                    } else {
                        mCurrentDistrict = "";
                    }
                    break;
            }
        }
    };

    public interface chooseCitySuccessListener {
        void onChooseCitySuccess(String mCurrentProviceName, String mCurrentCityName, String resmCurrentDistrictNameultStatus);

        void onChooseCityFail();
    }
}
